<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆级别" prop="carLevelId">
        <el-select v-model="queryParams.carLevelId" placeholder="请选择车辆级别" clearable>
          <el-option
            v-for="dict in dict.type.t_car_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车型描述" prop="descr">
        <el-input
          v-model="queryParams.descr"
          placeholder="请输入车型描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车脑品牌" prop="brainBrand">
        <el-input
          v-model="queryParams.brainBrand"
          placeholder="请输入车脑品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['boss:vehicle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['boss:vehicle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['boss:vehicle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['boss:vehicle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="vehicleId" />
      <el-table-column label="城市" align="center" prop="cityId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_dict_city" :value="scope.row.cityId"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆级别" align="center" prop="carLevelId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_car_level" :value="scope.row.carLevelId"/>
        </template>
      </el-table-column>
      <el-table-column label="车型描述" align="center" prop="descr" />
      <el-table-column label="车辆行驶总里程" align="center" prop="runTotalNum" />
      <el-table-column label="车辆颜色" align="center" prop="carColor" />
      <el-table-column label="电池续航剩余" align="center" prop="batteryResidue" />
      <el-table-column label="选择车型" align="center" prop="carTypeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_vehicle_type" :value="scope.row.carTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆VIN码" align="center" prop="carVin" />
      <el-table-column label="车牌号" align="center" prop="carCode" />
      <el-table-column label="是否安装车脑" align="center" prop="brainId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.brand_tui" :value="scope.row.brainId"/>
        </template>
      </el-table-column>
      <el-table-column label="车脑品牌" align="center" prop="brainBrand" />
      <el-table-column label="车脑编号" align="center" prop="brainCode" />
      <el-table-column label="大屏品牌" align="center" prop="screenBrand" />
      <el-table-column label="大屏编号" align="center" prop="screenCode" />
      <el-table-column label="车辆状态" align="center" prop="carStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_vehicle_status" :value="scope.row.carStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否绑定司机" align="center" prop="isBind">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.brand_tui" :value="scope.row.isBind"/>
        </template>
      </el-table-column>
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="司机手机号" align="center" prop="driverPhone" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['boss:vehicle:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['boss:vehicle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="城市" prop="cityId">
          <el-select v-model="form.cityId" placeholder="请选择城市">
            <el-option
              v-for="dict in dict.type.t_dict_city"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆级别" prop="carLevelId">
          <el-select v-model="form.carLevelId" placeholder="请选择车辆级别">
            <el-option
              v-for="dict in dict.type.t_car_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车型描述" prop="descr">
          <el-input v-model="form.descr" placeholder="请输入车型描述" />
        </el-form-item>
        <el-form-item label="车辆行驶总里程" prop="runTotalNum">
          <el-input v-model="form.runTotalNum" placeholder="请输入车辆行驶总里程" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="carColor">
          <el-input v-model="form.carColor" placeholder="请输入车辆颜色" />
        </el-form-item>
        <el-form-item label="电池续航剩余" prop="batteryResidue">
          <el-input v-model="form.batteryResidue" placeholder="请输入电池续航剩余" />
        </el-form-item>
        <el-form-item label="选择车型" prop="carTypeId">
          <el-select v-model="form.carTypeId" placeholder="请选择选择车型">
            <el-option
              v-for="dict in dict.type.t_vehicle_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆VIN码" prop="carVin">
          <el-input v-model="form.carVin" placeholder="请输入车辆VIN码" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carCode">
          <el-input v-model="form.carCode" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="是否安装车脑" prop="brainId">
          <el-radio-group v-model="form.brainId">
            <el-radio
              v-for="dict in dict.type.brand_tui"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车脑品牌" prop="brainBrand">
          <el-input v-model="form.brainBrand" placeholder="请输入车脑品牌" />
        </el-form-item>
        <el-form-item label="车脑编号" prop="brainCode">
          <el-input v-model="form.brainCode" placeholder="请输入车脑编号" />
        </el-form-item>
        <el-form-item label="大屏品牌" prop="screenBrand">
          <el-input v-model="form.screenBrand" placeholder="请输入大屏品牌" />
        </el-form-item>
        <el-form-item label="大屏编号" prop="screenCode">
          <el-input v-model="form.screenCode" placeholder="请输入大屏编号" />
        </el-form-item>
        <el-form-item label="车辆状态" prop="carStatus">
          <el-radio-group v-model="form.carStatus">
            <el-radio
              v-for="dict in dict.type.t_vehicle_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否绑定司机" prop="isBind">
          <el-radio-group v-model="form.isBind">
            <el-radio
              v-for="dict in dict.type.brand_tui"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="司机手机号" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicle, getVehicle, delVehicle, addVehicle, updateVehicle } from "@/api/boss/vehicle"

export default {
  name: "Vehicle",
  dicts: ['t_vehicle_type', 't_vehicle_status', 't_dict_city', 'brand_tui', 't_car_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆列表表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carLevelId: null,
        descr: null,
        brainBrand: null,
        driverName: null,
        driverPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询车辆列表列表 */
    getList() {
      this.loading = true
      listVehicle(this.queryParams).then(response => {
        this.vehicleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        cityId: null,
        carLevelId: null,
        descr: null,
        runTotalNum: null,
        carColor: null,
        batteryResidue: null,
        carTypeId: null,
        carVin: null,
        carCode: null,
        brainId: null,
        brainBrand: null,
        brainCode: null,
        screenBrand: null,
        screenCode: null,
        carStatus: null,
        isBind: null,
        driverName: null,
        driverPhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加车辆列表"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const vehicleId = row.vehicleId || this.ids
      getVehicle(vehicleId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改车辆列表"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != null) {
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids
      this.$modal.confirm('是否确认删除车辆列表编号为"' + vehicleIds + '"的数据项？').then(function() {
        return delVehicle(vehicleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('boss/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
